;;; init-minibuffer.el --- Description -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 gotpl
;;
;; Author: gotpl <631707329@qq.com>
;; Maintainer: gotpl <631707329@qq.com>
;; Created: 七月 27, 2023
;; Modified: 七月 27, 2023
;; Version: 0.0.1
;; Keywords: abbrev bib c calendar comm convenience data docs emulations extensions faces files frames games hardware help hypermedia i18n internal languages lisp local maint mail matching mouse multimedia news outlines processes terminals tex tools unix vc wp
;; Homepage: https://github.com/gotpl/init-minibuffer
;; Package-Requires: ((emacs "24.3"))
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;;  Description
;;
;;; Code:

(use-package marginalia
  :after vertico
  :config
  (marginalia-mode t)
  )
(use-package vertico
  :init
  (vertico-mode) ;; before minibufer
  :hook (
         (minibuffer-setup . vertico-repeat-save))
  ;; :custom
  ;; (vertico-sort-function nil) ;;设置为nil历史命令无法生效
)

(defun my-copy-minibuffer-contents (&optional tans-absolute-path)
  "Copy the contents of the minibuffer to the clipboard. tans-absolute-path if t, tansform to absolute path if content is path."
  (interactive)
  (when (minibufferp)
    (let ((contents (minibuffer-contents)))
      (when tans-absolute-path
        (setq contents (expand-file-name contents)))
      (kill-new contents)
      (message "Minibuffer contents copied: %s" contents))))

(defvar origin-minibuffer-selected-buffer nil)

(defun my-insert-minibuffer-contents (&optional tans-absolute-path)
  "Insert the contents of the minibuffer."
  (interactive)
  (when (minibufferp)
    (let ((contents (minibuffer-contents)))
      (when tans-absolute-path
        (setq contents (expand-file-name contents)))
      (with-current-buffer origin-minibuffer-selected-buffer 
        (insert contents))
      (abort-minibuffers))))

(defun my-insert-minibuffer-contents-abs-path ()
  (interactive)
  (my-insert-minibuffer-contents t))

(defun my-copy-minibuffer-contents-abs-path ()
  (interactive)
  (my-copy-minibuffer-contents t))
(defun my-setup-minibuffer ()
  (setq origin-minibuffer-selected-buffer (window-buffer (minibuffer-selected-window)))
  (define-key minibuffer-local-map (kbd "C-c C") 'my-copy-minibuffer-contents-abs-path)
  (define-key minibuffer-local-map (kbd "C-c c") 'my-copy-minibuffer-contents)
  (define-key minibuffer-local-map (kbd "C-c i") 'my-insert-minibuffer-contents)
  (define-key minibuffer-local-map (kbd "C-c I") 'my-insert-minibuffer-contents-abs-path)
  )

(defun my-exit-minibuffer ()
  (setq origin-minibuffer-selected-buffer nil)
  ;; 下面第二打开minibuffer会出现c键占用前缀
  ;; (define-key minibuffer-local-map (kbd "c-c C") nil)
  ;; (define-key minibuffer-local-map (kbd "c-c c") nil)
  ;; (define-key minibuffer-local-map (kbd "c-c I") nil)
  ;; (define-key minibuffer-local-map (kbd "c-c i") nil)
  )

(add-hook 'minibuffer-setup-hook 'my-setup-minibuffer)
(add-hook 'minibuffer-exit-hook 'my-exit-minibuffer)


(use-package wgrep
  :defer t)

(use-package embark
  :bind (:map minibuffer-local-map
              ("M-o"     . embark-act)
              ("C-c C-c" . embark-export)
              ("C-c C-o" . embark-collect)
              ("C-c C-e" . embark-export-write))
  :config
  (define-key embark-file-map (kbd "E") #'open-directory-externally)
  (define-key embark-file-map (kbd "y") #'gotpl/copy-file-path)
  :custom
  (prefix-help-command 'embark-prefix-help-command))

(use-package consult
  :bind (([remap imenu]                  . consult-imenu)
         ([remap goto-line]              . consult-goto-line)
         ([remap bookmark-jump]          . consult-bookmark)
         ([remap evil-show-marks]        . consult-mark)
         ([remap recentf-open-files]     . consult-recent-file)
         ([remap repeat-complex-command] . consult-complex-command)
         ("C-x b" . consult-buffer))
  :config
  (with-no-warnings
    (consult-customize consult-ripgrep consult-git-grep consult-grep
                       consult-bookmark
                       consult-recent-file
                       consult-buffer
                       ;; :preview-key nil ;; 禁止预览
                       ))
;; make consult-ripgrep work in windows
  (add-to-list 'process-coding-system-alist 
          '("[rR][gG]" . (utf-8-dos . windows-1251-dos)))
  ;; Windows设置locate为everything，需添加命令行程序到环境变量
  (if (and (eq system-type 'windows-nt)
	 (fboundp 'w32-shell-execute))
    (progn
  (setq consult-locate-args (encode-coding-string "es.exe -i -p -r" 'gbk))
  (add-to-list 'process-coding-system-alist '("es" gbk . gbk))
  ))
  :custom
  (consult-fontify-preserve nil)
  (consult-async-min-input 2)
  (consult-async-refresh-delay 0.15)
  (consult-async-input-throttle 0.2)
  (consult-async-input-debounce 0.1)
  :config
  (setq consult-ripgrep-args (concat consult-ripgrep-args " --hidden -g !*.git*")))

;; Consult users will also want the embark-consult package.
(use-package embark-consult
  :after (embark consult))
;; 支持拼音搜索
(use-package pyim
  :commands pyim-cregexp-build
  )

(defun eh-orderless-regexp (orig_func component)
  (let ((result (funcall orig_func component)))
      ;; (require 'pyim) ;; use-package lazy load
      (pyim-cregexp-build result)))

(defun toggle-chinese-search ()
    (interactive)
    (if (not (advice-member-p #'eh-orderless-regexp 'orderless-regexp))
    (advice-add 'orderless-regexp :around #'eh-orderless-regexp)
        (advice-remove 'orderless-regexp #'eh-orderless-regexp)))

(defun disable-py-search (&optional args)
    (if (advice-member-p #'eh-orderless-regexp 'orderless-regexp)
	(advice-remove 'orderless-regexp #'eh-orderless-regexp)))

;; (advice-add 'exit-minibuffer :after #'disable-py-search)
(add-hook 'minibuffer-exit-hook 'disable-py-search)

(provide 'init-minibuffer)
;;; init-minibuffer.el ends here
